package cn.didi.feidi.androidorder.adapter;

import android.content.Context;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.List;

import cn.didi.feidi.androidorder.Constants;
import cn.didi.feidi.androidorder.R;
import cn.didi.feidi.androidorder.model.MoneyUserLog;
import cn.didi.feidi.androidorder.util.AppUtil;

/**
 * Created by 独步清风 on 2017/7/25.
 */

public class PersonMoneyDetailListViewAdapter extends ArrayAdapter<MoneyUserLog> {

    private int resourceId;

    private String loadText="加载更多";

    private boolean isMore = true;

    public void setLoadText(String loadText) {
        this.loadText = loadText;
    }

    public void setMore(boolean more) {
        isMore = more;
    }

    public PersonMoneyDetailListViewAdapter(@NonNull Context context, @LayoutRes int resource, @NonNull List<MoneyUserLog> objects) {
        super(context, resource, objects);
        resourceId = resource;
    }


    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        MoneyUserLog balanceDetail = getItem(position); // 获取当前项的BalanceDetailItem实例

        View view;
        ViewHolder viewHolder;
        Log.d("test view", "start");
        if (convertView == null) {
            Log.d("test view", "convertView == null");
            view = LayoutInflater.from(getContext()).inflate(resourceId, null);
            viewHolder = new ViewHolder();
            viewHolder.businessSource = (TextView) view.findViewById(R.id.balance_detail_item_source);
            viewHolder.businessTime = (TextView) view.findViewById(R.id.balance_detail_item_time);
            viewHolder.businessValues = (TextView) view.findViewById(R.id.balance_detail_item_values);
            viewHolder.walletBalance = (TextView) view.findViewById(R.id.balance_detail_item_wallet_balance);

            viewHolder.llDetail = (LinearLayout) view.findViewById(R.id.balance_detail_item_detail);
            viewHolder.tvLoad = (TextView) view.findViewById(R.id.balance_detail_item_load);
            view.setTag(viewHolder); // 将ViewHolder存储在View中
        } else {
            Log.d("test view", "convertView != null");
            view = convertView;
            viewHolder = (ViewHolder) view.getTag(); // 重新获取ViewHolder
        }

        Log.d("test view", "viewHolder==null = " + (viewHolder == null));
        Log.d("test view", "viewHolder.businessSource==null = " + (viewHolder.businessSource == null));

        view.setVisibility(View.VISIBLE);
        if (position == getCount() - 1) {
            viewHolder.llDetail.setVisibility(View.GONE);
            if (isMore){
                viewHolder.tvLoad.setVisibility(View.VISIBLE);
                viewHolder.tvLoad.setText(loadText);
            }else {
                view.setVisibility(View.GONE);
                viewHolder.tvLoad.setVisibility(View.GONE);
            }

        } else {
            viewHolder.llDetail.setVisibility(View.VISIBLE);
            viewHolder.tvLoad.setVisibility(View.GONE);

            viewHolder.businessSource.setText(balanceDetail.getSource());
            viewHolder.businessTime.setText(AppUtil.dateTransferString(balanceDetail.getCreateTime(), Constants.RESPONSE_API_FORMAT_DATE));
            viewHolder.businessValues.setText(String.valueOf(balanceDetail.getBalanceChange()));
            viewHolder.walletBalance.setText(String.valueOf(balanceDetail.getBalance()));
        }
        return view;
    }

    class ViewHolder {
        TextView businessSource;
        TextView businessTime;
        TextView businessValues;
        TextView walletBalance;

        LinearLayout llDetail;
        TextView tvLoad;
    }
}
